<!--课目管理-->
<template>
  <div>
    <!--学历-->
    <div style="width: 30%;float: left">
      <el-card class="box-card" style="width: 100%">
        <div slot="header" class="clearfix">
          <span style="line-height: 100%;font-style: inherit">学历</span>
          <el-button style="float: right; padding: 3px 0;margin-left: 10px;" type="danger" @click="s_deleteLevelHandle">删除</el-button>
          <el-button style="float: right; padding: 3px 0" type="primary" @click="s_addLevelHandle">添加</el-button>
          <el-button style="float: right; padding: 3px 0" type="info" @click="s_editLevelHandle">编辑</el-button>
        </div>
        <el-table
          :data="lLevelTableData"
          @row-click="currentSelectedLevelRow"
          highlight-current-row
          style="width: 100%">
          <el-table-column
            prop="l_id"
            label="id"
            style="width: 30%">
          </el-table-column>
          <el-table-column
            prop="l_name"
            label="名称"
            style="width: 70%">
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!--年级-->
    <div style="width: 30%;float: left;margin-left: 20px;margin-right: 20px;">
      <el-card class="box-card" style="width: 100%">
        <div slot="header" class="clearfix">
          <span style="line-height: 100%;font-style: inherit">年级</span>
          <el-button style="float: right; padding: 3px 0;margin-left: 10px;" type="danger" @click="s_deleteGradeHandle">删除</el-button>
          <el-button style="float: right; padding: 3px 0" type="primary" @click="s_addGradeHandle">添加</el-button>
          <el-button style="float: right; padding: 3px 0" type="info" @click="s_editGradeHandle">编辑</el-button>
        </div>
        <el-table
          :data="gradeTableData"
          highlight-current-row
          @row-click="currentSelectedGradeRow"
          style="width: 100%">
          <el-table-column
            prop="g_id"
            label="id"
            style="width: 30%">
          </el-table-column>
          <el-table-column
            prop="g_name"
            label="名称"
            style="width: 70%">
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!--课目-->
    <div style="width: 30%;float: left">
      <el-card class="box-card" style="width: 100%">
        <div slot="header" class="clearfix">
          <span style="line-height: 100%;font-style: inherit">课目</span>
          <el-button style="float: right; padding: 3px 0;margin-left: 10px;" type="danger" @click="s_deleteSubjectHandle">删除</el-button>
          <el-button style="float: right; padding: 3px 0" type="primary" @click="s_addSubjectHandle">添加</el-button>
          <el-button style="float: right; padding: 3px 0" type="info" @click="s_editSubjectHandle">编辑</el-button>
        </div>
        <el-table
          :data="subjectTableData"
          highlight-current-row
          @row-click="currentSelectedSubjectRow"
          style="width: 100%">
          <el-table-column
            prop="sb_id"
            label="id"
            style="width: 30%">
          </el-table-column>
          <el-table-column
            prop="sb_name"
            label="名称"
            style="width: 70%">
          </el-table-column>
        </el-table>
      </el-card>
    </div>
   <edit-info v-if="isShowEditDialog" :isShowEditDialog="isShowEditDialog" :editData="editData" :editType="editType" :includeInfo="includeInfo" :includeNameInfo="includeNameInfo"  @closeEditDialog="closeEditDialog"></edit-info>
   <add-info v-if="isShowAddDialog" :isShowAddDialog="isShowAddDialog" :addData="addData" :addType="addType" :includeInfo="includeInfo" :includeNameInfo="includeNameInfo" @closeAddDialog="closeAddDialog"></add-info>
  </div>
</template>
<script>
  import {getAllSubject,addLevel,deleteLevel,addGrade,deleteGrade,addSubject,deleteSubject,findAllLevel,findAllGrade,findAllSubject} from '@/api/questions'
  import editInfo from './subjectComponent/editInfo.vue'
  import addInfo from  './subjectComponent/addInfo.vue'
  export default{
    components:{
      editInfo,
      addInfo
    },
    mounted(){
       this.s_getAllLevel();
    },
    data(){
      return {
        isShowEditDialog:false,
        isShowAddDialog:false,

        lLevelTableData: [],
        gradeTableData:[],
        subjectTableData:[],

        selectedLevelRow:null,//选中的学历信息
        selectedGradeRow:null,//选中的年级信息
        selectedSubjectRow:null, //选中的课目信息

        findGradeBySelectedLevelRow:null,//用于查询年级的学历信息
        findSubjectBySelectedGradeRow:null,//用户查询课目的年级信息

        editData:{
            id:0,
            name:""
        }, //要编辑的数据信息
        editType:0, //编辑信息的类型
        includeInfo:[], //所属类别
        includeNameInfo:[],

        addData:{
            id:0,
        },
        addType:0
      }
    },
    methods:{
        //获取所有学历
        s_getAllLevel(){
           findAllLevel().then(res=>{
             this.lLevelTableData = [];
             this.gradeTableData = [];
             this.subjectTableData = [];
               if(res.code == 200){
                   this.lLevelTableData = res.data;
                   if(this.lLevelTableData.length > 0){
                       this.s_getAllGradeByLId(this.lLevelTableData[0])
                   }
               }
           })
        },

        //获取指定学历下面的年级
       s_getAllGradeByLId(lLevel){
         findAllGrade(lLevel).then(res=>{
            this.gradeTableData = [];
            this.subjectTableData = [];
             if(res.code == 200){
                 this.gradeTableData = res.data;
                 if(this.gradeTableData.length > 0){
                     this.s_getAllSubjectByGId(this.gradeTableData[0])
                 }
             }
         })
       },

       //获取指定年级下面的所有科目
      s_getAllSubjectByGId(grade){
        this.subjectTableData = [];
        findAllSubject(grade).then(res=>{
            if(res.code == 200){
                this.subjectTableData = res.data;
            }
        })
      },

      //当学历的表格行被点击时
      currentSelectedLevelRow(row, event, column){

          this.selectedLevelRow = row;

          this.selectedGradeRow = null;
          this.selectedSubjectRow = null;

          this.findGradeBySelectedLevelRow = row;
          this.s_getAllGradeByLId(row)
      },

      //当年级的表格被点击时
      currentSelectedGradeRow(row, event, column){
        this.selectedGradeRow = row;
        this.selectedSubjectRow = null;
        this.findSubjectBySelectedGradeRow = row;
        this.s_getAllSubjectByGId(row)
      },

      //当课目的表格被点击时
      currentSelectedSubjectRow(row, event, column){
          this.selectedSubjectRow = row;
      },



      s_deleteLevelHandle(){
         if(this.selectedLevelRow == null){
           this.$message({
             type: 'warning',
             message: '请选择一行要删除的数据'
           });
         }else{
           this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
             confirmButtonText: '确定',
             cancelButtonText: '取消',
             type: 'warning'
           }).then(() => {
             deleteLevel(this.selectedLevelRow).then(res=>{
                 if(res.code == 200){
                   this.$message({
                     type: 'success',
                     message: '删除成功'
                   });
                   this.s_getAllLevel();
                 }else{
                   this.$message({
                     type: 'error',
                     message: '有关联的年级，不能删除'
                   });
                 }
             });
           }).catch(() => {

           });
         }
      },
      s_addLevelHandle(){
        this.includeInfo = [];
        this.includeInfo.push("学历");
        this.addType = 0;

        this.includeNameInfo = [];

        this.isShowAddDialog = true;
      },
      s_editLevelHandle(){
        if(this.selectedLevelRow != null){
          //要编辑的信息
          this.editData.id = this.selectedLevelRow.l_id;
          this.editData.name = this.selectedLevelRow.l_name;

          //编辑的类型
          this.editType = 0;

          //所属类别
          this.includeInfo = [];
          this.includeInfo.push("学历");

          this.includeNameInfo = [];

          this.isShowEditDialog = true
        }else{
          this.$message({
            type: 'warning',
            message: '请选择一行要编辑的数据'
          });
        }
      },



      s_deleteGradeHandle(){
        if(this.selectedGradeRow == null){
          this.$message({
            type: 'warning',
            message: '请选择一行要删除的数据'
          });
        }else{
          this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteGrade(this.selectedGradeRow).then(res=>{
              if(res.code == 200){
                this.$message({
                  type: 'success',
                  message: '删除成功'
                });
                if(this.findGradeBySelectedLevelRow == null){
                  this.findGradeBySelectedLevelRow = this.lLevelTableData[0];
                }
                this.s_getAllGradeByLId(this.findGradeBySelectedLevelRow)
              }else{
                this.$message({
                  type: 'error',
                  message: '有关联的课目，不能删除'
                });
              }
            });
          }).catch(() => {

          });
        }
      },
      s_addGradeHandle(){
        if(this.selectedLevelRow != null){

           this.addData.id = this.selectedLevelRow.l_id;

          this.includeInfo = [];
          this.includeInfo.push("学历","年级");

          this.includeNameInfo = [this.selectedLevelRow.l_name];

          this.addType = 1;

          this.isShowAddDialog = true;
        }else{
          this.$message({
            type: 'warning',
            message: '请选中对应的学历'
          });
        }
      },
      s_editGradeHandle(){
        if(this.selectedLevelRow != null){
          if(this.selectedGradeRow != null){
            //要编辑的信息
            this.editData.id = this.selectedGradeRow.g_id;
            this.editData.name = this.selectedGradeRow.g_name;

            //编辑的类型
            this.editType = 1;

            //所属类别
            this.includeInfo = [];
            this.includeInfo.push("学历","年级");

            this.includeNameInfo = [this.selectedLevelRow.l_name];

            this.isShowEditDialog = true
          }else{
            this.$message({
              type: 'warning',
              message: '请选择一行要编辑的数据'
            });
          }
        }else{
          this.$message({
            type: 'warning',
            message: '请选中对应的学历'
          });
        }
      },



      s_deleteSubjectHandle(){
        if(this.selectedSubjectRow == null){
          this.$message({
            type: 'warning',
            message: '请选择一行要删除的数据'
          });
        }else{
          this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteSubject(this.selectedSubjectRow).then(res=>{
              if(res.code == 200){
                this.$message({
                  type: 'success',
                  message: '删除成功'
                });
                if(this.findSubjectBySelectedGradeRow == null){
                  this.findSubjectBySelectedGradeRow = this.gradeTableData[0];
                }
                this.s_getAllSubjectByGId(this.findSubjectBySelectedGradeRow)
              }
            });
          }).catch(() => {

          });
        }
      },
      s_addSubjectHandle(){
          if(this.selectedLevelRow != null){
            if(this.selectedGradeRow != null){
              this.addData.id = this.selectedGradeRow.g_id;

              this.includeInfo = [];
              this.includeInfo.push("学历","年级","课目");

              this.includeNameInfo = [this.selectedLevelRow.l_name,this.selectedGradeRow.g_name];

              this.addType = 2;

              this.isShowAddDialog = true;
            }else{
              this.$message({
                type: 'warning',
                message: '请选中对应的年级'
              });
            }
          }else{
            this.$message({
              type: 'warning',
              message: '请选中对应的学历'
            });
          }
      },
      s_editSubjectHandle(){
          if(this.selectedLevelRow != null){
              if(this.selectedGradeRow != null){
                  if(this.selectedSubjectRow != null){
                    //要编辑的信息
                    this.editData.id = this.selectedSubjectRow.sb_id;
                    this.editData.name = this.selectedSubjectRow.sb_name;

                    //编辑的类型
                    this.editType = 2;

                    //所属类别
                    this.includeInfo = [];
                    this.includeInfo.push("学历","年级","课目");

                    this.includeNameInfo = [this.selectedLevelRow.l_name,this.selectedGradeRow.g_name];

                    this.isShowEditDialog = true
                  }else{
                    this.$message({
                      type: 'warning',
                      message: '请选中一行数据'
                    });
                  }
              }else{
                this.$message({
                  type: 'warning',
                  message: '请选中对应的年级'
                });
              }
          }else{
            this.$message({
              type: 'warning',
              message: '请选中对应的学历'
            });
          }
      },

      closeEditDialog(type){
          this.isShowEditDialog = false;
          if(type == 0){
            this.s_getAllLevel();
          }else if(type == 1){
            this.s_getAllGradeByLId(this.selectedLevelRow);
          }else if(type == 2){
            this.s_getAllSubjectByGId(this.selectedGradeRow)
          }else{
            this.s_getAllLevel();
          }
      },

      closeAddDialog(type){
        this.isShowAddDialog = false;
        if(type == 0){
          this.s_getAllLevel();
        }else if(type == 1){
          this.s_getAllGradeByLId(this.selectedLevelRow);
        }else if(type == 2){
            this.s_getAllSubjectByGId(this.selectedGradeRow)
        }else{
          this.s_getAllLevel();
        }
      }


    }
  }
</script>
<style scoped>
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

</style>
